
package nl.hanze.shoestring.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.html.HTMLEditorKit;
import nl.hanze.shoestring.domain.Reistype;
import nl.hanze.shoestring.logic.Logic;

/**
 * Beheer Reistype tabel
 * 
 * @author bleker
 */

public class ReistypePanel extends JPanel{
    
        Logic logic;
        JPanel pQueryInput, pRecord, pNavigation, pDbGrid;
        JLabel lblZoekSleutel, lblId, lblReistypeNaam, lblReistypeBeschrijving;
        JTextField txtZoekSleutel, txtId, txtReistypeNaam;
        JEditorPane txtReistypeBeschrijving;
        JButton butZoeken, butSave, butDelete, butEmpty;
        JTable table;
        JScrollPane scrollPaneRes;
        DefaultTableModel model;
        Reistype reistypeObj=null;
        List<Reistype> reistypeList;
        private final JScrollPane txtReistypeBeschrijvingScrollable;

        int heightField = 25;
        int numberOfRows = 19;

        public ReistypePanel(Logic logic) {
            this.logic = logic;
 
        table = new JTable();
        setLayout(new BorderLayout(20, 20));

        // input velden north sectie
//        pQueryInput = new JPanel();
//        pQueryInput.setLayout(null);
//        pQueryInput.setPreferredSize(new Dimension(400, 60));
//
//        lblZoekSleutel = UIHelper.createLabel(lblZoekSleutel = new JLabel(), "Zoeksleutel", 10, 10, 150, heightField, pQueryInput);
//        txtZoekSleutel = UIHelper.createTextField(txtZoekSleutel = new JTextField(), "", 170, 10, 250, heightField, pQueryInput);
//
//        butZoeken = UIHelper.createButton(butZoeken = new JButton(), "Zoeken", 430, 10, 100, heightField, pQueryInput);
//        butZoeken.addActionListener(
//                new ActionListener() {
//                    @Override
//                    public void actionPerformed(ActionEvent e) {
//                        System.out.println("zoek");
//                    }
//                });

        // Center sectie, database record details
        pRecord = new JPanel();
        pRecord.setLayout(null);
        pRecord.setPreferredSize(new Dimension(400, 530));
        pRecord.setBorder(BorderFactory.createLineBorder(Color.blue));
        
        lblId = UIHelper.createLabel(lblId = new JLabel(), "Nummer", 20, 10, 150, heightField, pRecord);
        txtId = UIHelper.createTextField(txtId = new JTextField(), "", 170, 10, 100, heightField, pRecord);

        lblReistypeNaam = UIHelper.createLabel(lblReistypeNaam = new JLabel(), "ReistypeNaam", 20, 70, 150, heightField, pRecord);
        txtReistypeNaam = UIHelper.createTextField(txtReistypeNaam = new JTextField(), "", 170, 70, 500, heightField, pRecord);

        lblReistypeBeschrijving = UIHelper.createLabel(lblReistypeBeschrijving = new JLabel(), "ReistypeBeschrijving", 20, 100, 150, heightField, pRecord);
        txtReistypeBeschrijving = new JEditorPane();
        txtReistypeBeschrijving.setEditorKit(new HTMLEditorKit());
        txtReistypeBeschrijvingScrollable = new JScrollPane(txtReistypeBeschrijving);
        txtReistypeBeschrijvingScrollable.setBounds(170, 100, 700, 100);
        pRecord.add(txtReistypeBeschrijvingScrollable);
        
        butSave = UIHelper.createButton(butSave = new JButton(), "Opslaan", 170, 220, 120, heightField, pRecord);
        butSave.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("save");
                        if(reistypeObj!=null) {
                            updateRecord(reistypeObj);
                        } else {
                            addRecord();
                        }
                    }
                });


        butDelete = UIHelper.createButton(butDelete = new JButton(), "Verwijderen", 350, 220, 120, heightField, pRecord);
        butDelete.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("delete");
                        if(reistypeObj!=null) {
                            deleteRecord(reistypeObj);
                        }
                }});
        butDelete.setVisible(false);

        butEmpty = UIHelper.createButton(butDelete = new JButton(), "Leeg/Nieuw", 350, 10, 120, heightField, pRecord);
        butEmpty.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        emptyAll();
                }});
        
        // fill tab pane
        add(pRecord, BorderLayout.CENTER);
//        add(pQueryInput, BorderLayout.NORTH);
        addTable();
        emptyAll();
    }

    /**
     * Haal Reistype informatie uit database en toon in JTable
     */
    private void addTable() {

        // Gridcontainer
        pDbGrid = new JPanel(new BorderLayout(2, 3));

        // scroll pane eigenschappen
        scrollPaneRes = new JScrollPane(table);
        Dimension d = table.getPreferredSize();
        scrollPaneRes.setPreferredSize(new Dimension(d.width, table.getRowHeight() * numberOfRows));

        // navigatie container
        UIHelper.addNavigation(pNavigation = new JPanel(), table, scrollPaneRes, numberOfRows);
        
        // kolom eigenschappen - breedte
        Object[] headers = {"Nummer", "Reistype", "Beschrijving"};
        int[] breedte = {100, 300, 600};
        
        // tablemodel en eigenschappen instellen, kolombreedte etc
        model = new DefaultTableModel(headers, 0) {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
            
            // rendering van cellen, toont checkbox en juiste datum indien datatype overeenkomstig is
            // null velden veroorzaken fout, standaard maar op String gezet....
            @Override
            public Class getColumnClass(int column){
                try {
                 return getValueAt(0, column).getClass();
                } catch (java.lang.NullPointerException e) {
                    return String.class;
                }
            }

        };

        table.setModel(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        for (int i0 = 0; i0 < breedte.length; i0++) {
            table.getColumnModel().getColumn(i0).setPreferredWidth(breedte[i0]);
        }
        
        updateTable(true);

        // selecteer row door muisklik    
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                reistypeObj = reistypeList.get(table.convertRowIndexToModel(table.getSelectedRow()));
                getRecord(reistypeObj);
            }
        });

        pDbGrid.add(scrollPaneRes, BorderLayout.CENTER);
        pDbGrid.add(pNavigation, BorderLayout.SOUTH);
        add(pDbGrid, BorderLayout.SOUTH);
    }
    
    
    /*
     *  Vul detailvelden met geselecteerde rowdata  
     */
    private void getRecord(Reistype reistypeObj) {
        txtId.setText("" + reistypeObj.getId());
        txtId.setEditable(false);
        txtReistypeNaam.setText("" + reistypeObj.getNaam());
        txtReistypeBeschrijving.setText("" + reistypeObj.getBeschrijving());
    }
    
    /*
     *  update record  
     */
    private void updateRecord(Reistype reistypeObj) {
        reistypeObj.setNaam(txtReistypeNaam.getText());
        reistypeObj.setBeschrijving(txtReistypeBeschrijving.getText());
        logic.updateReistype(reistypeObj);
        updateTable(true);
        emptyAll();
    }
    
    /*
     *  add new record  
     */
    private void addRecord() {
        reistypeObj = new Reistype();
        reistypeObj.setNaam(txtReistypeNaam.getText());
        reistypeObj.setBeschrijving(txtReistypeBeschrijving.getText());
        logic.addReistype(reistypeObj);
        updateTable(true);
        emptyAll();
    }

    /*
     *  delete record  
     */
    private void deleteRecord(Reistype reistypeObj) {
        logic.deleteReistype(reistypeObj);
        updateTable(true);
        emptyAll();
    }
    
    /*
     *  maak velden leeg en reistypeObj =null  
     */
    private void emptyAll() {
        txtId.setEditable(false);
        txtId.setText("");
        txtReistypeNaam.setText("");
        txtReistypeBeschrijving.setText("");
        reistypeObj=null;
    }
    
    private void updateTable(Boolean refresh) {
        
        if(refresh) {
            reistypeList = logic.getReistypeList();
        }
        model.setRowCount(0);

        // data in JTable plaatsen
        for (Reistype reistype : reistypeList) {
            Object[] o = {reistype.getId(),
                reistype.getNaam(),
                reistype.getBeschrijving()};
            model.addRow(o);
        }
    } 
}
